package com.czyxy.controller;

import com.czyxy.domain.Clazz;
import com.czyxy.domain.StudentFee;
import com.czyxy.repository.OtherRepository;
import com.czyxy.repository.StudentFeeRepository;
import com.czyxy.services.JsonService;
import com.czyxy.services.UserService;
import com.czyxy.utils.JsonResult;
import com.czyxy.utils.OperationType;
import com.czyxy.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

/**
 * Created by mike on 6/9/17.
 */
@RestController
public class StudentFeeQueryController {
    @Autowired
    private JsonService jsonService;
    @Autowired
    private StudentFeeRepository repository;
    @Autowired
    private OtherRepository otherRepository;
    @Autowired
    private UserService userService;

    @RequestMapping("/upload")
    public JsonResult fileUpload(
            @RequestParam("file") MultipartFile file,
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            HttpServletRequest req) {

        try {
            userService.validateUser(username,password, OperationType.UPLOAD);
        } catch (Exception e) {
            return new JsonResult(false, e.getMessage(),null);
        }
        try {
            File temfile = File.createTempFile("tmp", "json");
            file.transferTo(temfile);
            List<StudentFee> fees = jsonService.readJsonFile(temfile);
            repository.deleteAllInBatch();
//            fees.forEach(studentFee -> {
//                repository.save(studentFee);
//            });
            List result = repository.save(fees);
            return new JsonResult(true, "导入数据成功!总计[" + result.size() + "]条数据!");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "导入数据失败!失败原因;[" + e.getMessage() + "]");
        }


    }

    @RequestMapping(value = "/queryByStudent")
    public JsonResult queryByStudent(@RequestParam("studentCode") String studentCode, @RequestParam("studentName") String studentName, @RequestParam("pageNumber") int pageNumber,
                                     @RequestParam("pageSize") int pageSize) {
        Pageable pageable = new PageRequest(pageNumber, pageSize);
        Page page = repository.findAllByStudentCodeAndStudentName(studentCode, studentName, pageable);

        return new JsonResult(true, "数据查询成功!", new PageResult((int) page.getTotalElements(), page.getContent()));
    }


    @RequestMapping(value = "/teacherQuery")
    public JsonResult teacherQuery(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            @RequestParam("classCode") String classCode,
            @RequestParam("className") String className,
            @RequestParam("studentCode") String studentCode,
            @RequestParam("studentName") String studentName,
            @RequestParam("pageNumber") int pageNumber,
            @RequestParam("pageSize") int pageSize,
            @RequestParam("qf") boolean qf
    ) {
        try {
            userService.validateUser(username,password, OperationType.TQUERY);
        } catch (Exception e) {
            return new JsonResult(false, e.getMessage());
        }
        Pageable pageable = new PageRequest(pageNumber, pageSize);
        Page page = repository.teacherQuery(classCode, className, studentCode, studentName, qf, pageable);

        return new JsonResult(true, "数据查询成功!", new PageResult((int) page.getTotalElements(), page.getContent()));
    }

    @RequestMapping("/getClasses")
    public List<Clazz> test(@RequestParam("helpcode") String helpcode) {
        return otherRepository.getClassesByHelpcode(helpcode);
    }

}
